package com.mingqijia.gassafety.console.controller.inner;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.db.entity.WorkOrderHistory;
import com.mingqijia.gassafety.shared.constant.AppealStateEnum;
import com.mingqijia.gassafety.webserver.request.NewAsstCreateRequest;
import com.mingqijia.gassafety.webserver.request.NewAsstUnbindRequest;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.workorder.NewAsstEditContactRequest;
import com.mingqijia.gassafety.webserver.request.workorder.SparkOrderRequest;
import com.mingqijia.gassafety.webserver.request.workorder.SparkOrderStatusRequest;
import com.mingqijia.gassafety.webserver.request.workorder.mbp.EquipmentInfoRequest;
import com.mingqijia.gassafety.webserver.response.client.ConcentrationCurveRes;
import com.mingqijia.gassafety.webserver.response.workorder.mbp.BrokenLogResponse;
import com.mingqijia.gassafety.webserver.response.workorder.mbp.InfoAndAlertLogResponse;
import com.mingqijia.gassafety.webserver.service.ContactsService;
import com.mingqijia.gassafety.webserver.service.HistoryLogService;
import com.mingqijia.gassafety.webserver.service.workorder.SyncNewAsstOrderService;
import com.mingqijia.gassafety.webserver.service.workorder.WorkOrderHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName SyncNewAsstOrderController.java
 * @Description 同步新时刻助手工单信息
 * @createTime 2023年10月20日
 */
@Slf4j
@Api(tags = "同步新时刻助手工单信息")
@RestController(value = "SyncNewAsstOrderController")
@RequestMapping("/inner/v1/newAsst/sync/order")
public class SyncNewAsstOrderController {

    @Autowired
    private SyncNewAsstOrderService service;

    @Autowired
    HistoryLogService historyLogService;

    @Autowired
    WorkOrderHistoryService workOrderHistoryService;




    @ApiOperation("查询设备信息、报警记录")
    @RequestMapping(value = "getInfoWithAlertLog", method = RequestMethod.POST)
    public CommonResponse<Object> getInfoWithAlertLog(@RequestBody @Valid EquipmentInfoRequest request) {
        InfoAndAlertLogResponse response =  service.getEquipmentInfo(request);
        return CommonResponse.success(response);
    }

    @ApiOperation("故障记录")
    @RequestMapping(value = "brokenLog", method = RequestMethod.POST)
    public CommonResponse<Object> brokenLog(@RequestBody @Valid EquipmentInfoRequest request) {
        PageUtil<BrokenLogResponse> response =  service.getBrokenLog(request);
        return CommonResponse.success(response);
    }

    @ApiOperation("报警浓度曲线")
    @RequestMapping(value = { "alarm/log"}, method = RequestMethod.GET)
    public CommonResponse<List<ConcentrationCurveRes>> historyLog(@RequestParam @ApiParam(value = "设备号") String imei,
                                                                  @RequestParam @ApiParam(value = "开始时间") String timeStart,
                                                                  @RequestParam @ApiParam(value = "结束时间") String timeEnd){
        if (StringUtils.isEmpty(imei)) return CommonResponse.failure("设备号不能为空！");
        if (StringUtils.isEmpty(timeStart)) return CommonResponse.failure("开始时间不能为空！");
        if (StringUtils.isEmpty(timeEnd)) return CommonResponse.failure("结束时间不能为空！");
        return CommonResponse.success(historyLogService.getConcentrationCurve(imei,timeStart,timeEnd));
    }

    @ApiOperation("绑定设备")
    @RequestMapping(value = "bindingEquipment", method = RequestMethod.POST)
    public CommonResponse<Object> bindingDtu(@RequestBody @Valid NewAsstCreateRequest request) {
        service.createConsumerBindImei(request);
        return CommonResponse.success("success");
    }

    @ApiOperation("同步设备解绑状态")
    @RequestMapping(value = "unbindEquipment", method = RequestMethod.POST)
    public CommonResponse<Object> unbindEquipment(@RequestBody @Valid NewAsstUnbindRequest request) {
        service.unbindEquipment(request);
        return CommonResponse.success("success");
    }

    @ApiOperation("修改联系人信息")
    @RequestMapping(value = "contact/edit", method = RequestMethod.POST)
    public CommonResponse<Object> editContacts(@RequestBody @Valid NewAsstEditContactRequest request){
        service.editContacts(request);
        return CommonResponse.success("sucess");
    }

    @ApiOperation("创建/取消点火,安检工单")
    @RequestMapping(value = "spark" , method = RequestMethod.POST)
    public CommonResponse<Object> spark(@RequestBody @Valid SparkOrderRequest request){
        Integer state;
        if( null == request.getState() ){// 查询调试状态
            state = service.sparkStatus(request);
        }else{ // 创建/取消点火,安检工单
            state = service.spark(request);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("state",state);
        return CommonResponse.success(map);
    }
}